package com.gxa.modules.payment.service.impl;

import com.gxa.modules.payment.entity.Bill;
import com.gxa.modules.payment.form.*;
import com.gxa.modules.payment.mapper.*;
import com.gxa.modules.payment.service.BillService;
import com.gxa.modules.payment.util.DocNoGenerationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private BillStatusMapper billStatusMapper;

    @Autowired
    private MeterDetailsFormMapper meterDetailsFormMapper;

    @Autowired
    private PayItemMapper payItemMapper;

    @Autowired
    private BillQuerySomeFormMapper billQuerySomeFormMapper;

    @Autowired
    private DocNoGenerationUtil docNoGenerationUtil;

    @Override
    public List<BillStatusForm> queryByStatus() {
        List<BillStatusForm> billStatusForms = this.billStatusMapper.queryByStatus();
        return billStatusForms;
    }

    @Override
    public List<Bill> queryByPage(Map<String, Object> param) {
        List<Bill> bills = this.billMapper.queryByPage(param);
        return bills;
    }

//    @Override
//    public Integer addNo() {
//        Integer receiptNo = this.docNoGenerationUtil.generateDocNo(2);
//        return receiptNo;
//    }

    @Override
    public void add(BillAddForm billAddForm) {
        Integer meterDetailsId = billAddForm.getId();
        List<MeterDetailsForm> meterDetailsForms = this.meterDetailsFormMapper.queryById(meterDetailsId);

        for(MeterDetailsForm meterDetailsForm : meterDetailsForms){
            //获得 起始读表
            double startMeter = meterDetailsForm.getStartMeter();
            //获得 结束读表
            double endMeter = meterDetailsForm.getEndMeter();
            //获得 单价
            double price = meterDetailsForm.getPrice();
            //本期用度
            double quantity = endMeter - startMeter;
            //费用合计
            double money = quantity * price + 120;

//            String formula = billAddForm.getFormula();
//            Integer unitId = billAddForm.getUnitId();
//            Integer accuracyId = billAddForm.getAccuracyId();
//            Integer lateFee = billAddForm.getLateFee();
            List<PayItemAddBillForm> payItemAddBillForms = billAddForm.getPayItemAddBillForms();
            for(PayItemAddBillForm payItemAddBillForm : payItemAddBillForms){

                String formula = payItemAddBillForm.getFormula();
                System.out.println(formula);
                System.out.println(price);

                this.payItemMapper.addAndBill(payItemAddBillForm);

                Integer integer = payItemAddBillForm.getPayItemId();
                billAddForm.setPayItemId(integer);
            }

            Integer receiptNo = this.docNoGenerationUtil.generateDocNo(2);
            billAddForm.setCode(receiptNo);
            billAddForm.setQuantity(quantity);
            billAddForm.setMoney(money);
        }

        this.billMapper.add(billAddForm);
    }

    @Override
    public List<BillQuerySomeForm> queryByCode(Integer code) {
        System.out.println(code);
        List<BillQuerySomeForm> billQuerySomeForms = this.billQuerySomeFormMapper.queryByCode(code);
        return billQuerySomeForms;
    }

    @Override
    public void update(BillUpdateForm billUpdateForm) {
        Integer lateFee = billUpdateForm.getLateFee();
        this.payItemMapper.addAndBillSome(lateFee);

        this.billMapper.update(billUpdateForm);
    }

    @Override
    public void delete(Integer code) {
        this.billMapper.delete(code);
    }

    @Override
    public void billDeleteIds(List<Integer> ids) {
        this.billMapper.billDeleteIds(ids);
    }

}
